//
//  WithdrawListViewController.m
//  AllFamily
//
//  Created by chirui on 2018/6/6.
//  Copyright © 2018年 Shui. All rights reserved.
//

#import "WithdrawListViewController.h"
#import "GetMoneyDetailsCell.h"

@interface WithdrawListViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
    UIView * noBodyView;
}

@end

@implementation WithdrawListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    _dataArray = @[].mutableCopy;
    _page =1;
    [self getListData];
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.titleView=[TopLabel withLabelWithLabelText:@"提现记录"];
    [self creatTableView];

    noBodyView = [NothingMessageView creatNothingMessageViewFrame:CGRectMake(WIDTH/2-100, (HEIGHT-64)/2-150, 200, 200) andMessageStr:@"没有相关内容！" onView:nil];

    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_renter" target:self action:@selector(backBtnClick)];
}

-(void)backBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];

    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor =[UIColor whiteColor];
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[GetMoneyDetailsCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[GetMoneyDetailsCell reuseIdentifier]];
    
    
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [_tableView.mj_header endRefreshing];
        if (verifTokenAndEnter) {
            _page = 1;
            [self getListData];
        }else {
            
            [_tableView.mj_header endRefreshing];
            
            SHuiMBhudTextTimeStr(@"请先登录您的账号！");
            
            return;
        }
    }];
    _tableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (verifTokenAndEnter) {
            _page ++;
            [self getListData];
        }else {
            
            [_tableView.mj_footer endRefreshing];
            
            SHuiMBhudTextTimeStr(@"请先登录您的账号！");
            
            return;
        }
        
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    
    if (_dataArray.count>10) {
        _tableView.mj_footer.hidden = NO;
    }else{
        _tableView.mj_footer.hidden = YES;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.01;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    GetMoneyDetailsCell  * cell = [tableView dequeueReusableCellWithIdentifier:[GetMoneyDetailsCell reuseIdentifier] forIndexPath:indexPath];
    cell.statisLabel.text = dic[@"wit_status"];
    cell.timeLabel.text = dic[@"create_time"];

        cell.moneyLabel.text = [NSString stringWithFormat:@"￥%@",dic[@"wit_money"]];

    
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)getListData{
    
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];

            return ;
        }
    }
    NSDictionary * dic = @{
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"perpage":@"10",
                           @"mid":USER_DEFAULTGet(@"mid"),
                           @"token":USER_DEFAULTGet(@"token")
                           };

    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(get_withDrawList_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage = [responseObject[@"total_page"]  intValue];

            if (_page==1) {

                if (_dataArray) {

                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {

                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"] ) {

                        [_dataArray addObject:dic];
                    }
                }
            }else {

                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {

                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"] ) {

                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{

    if (_dataArray.count>0) {
        noBodyView.hidden =YES;
        if (_dataArray.count<10) {

            _tableView.mj_footer.hidden = YES;
        }else {
            _tableView.mj_footer.hidden = NO;
        }
    }else {
        _tableView.mj_footer.hidden = YES;
        noBodyView.hidden =NO;

        [self.view addSubview:noBodyView];
    }
    [_tableView.mj_header endRefreshing];
    [_tableView.mj_footer endRefreshing];
    [_tableView reloadData];
            });

        }else {

            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }

    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}
@end
